USE [ShortenUrl]
GO

/****** Object:  Table [dbo].[ShortenUrls]    Script Date: 11/14/2012 16:41:06 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

SET ANSI_PADDING ON
GO

IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[ShortenUrls]') AND type in (N'U'))
BEGIN
CREATE TABLE [dbo].[ShortenUrls](
	[Id] [int] IDENTITY(1,1) NOT NULL,
	[OriginalUrl] [varchar](1000) NOT NULL,
	[ShortUrlCode] [varchar](50) NOT NULL,
	[UserId] [int] NULL,
	[InsertDate] [datetime] NOT NULL,
 CONSTRAINT [PK_ShortenUrls] PRIMARY KEY CLUSTERED 
(
	[Id] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]
END
GO

SET ANSI_PADDING OFF
GO

IF NOT EXISTS (SELECT * FROM sys.foreign_keys WHERE object_id = OBJECT_ID(N'[dbo].[FK_ShortenUrls_ShortenUsers]') AND parent_object_id = OBJECT_ID(N'[dbo].[ShortenUrls]'))
ALTER TABLE [dbo].[ShortenUrls]  WITH NOCHECK ADD  CONSTRAINT [FK_ShortenUrls_ShortenUsers] FOREIGN KEY([UserId])
REFERENCES [dbo].[ShortenUsers] ([Id])
GO

IF  EXISTS (SELECT * FROM sys.foreign_keys WHERE object_id = OBJECT_ID(N'[dbo].[FK_ShortenUrls_ShortenUsers]') AND parent_object_id = OBJECT_ID(N'[dbo].[ShortenUrls]'))
ALTER TABLE [dbo].[ShortenUrls] NOCHECK CONSTRAINT [FK_ShortenUrls_ShortenUsers]
GO

IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[DF_ShortenUrls_UserId]') AND type = 'D')
BEGIN
ALTER TABLE [dbo].[ShortenUrls] ADD  CONSTRAINT [DF_ShortenUrls_UserId]  DEFAULT (NULL) FOR [UserId]
END

GO

IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[DF_ShortenUrls_InsertDate]') AND type = 'D')
BEGIN
ALTER TABLE [dbo].[ShortenUrls] ADD  CONSTRAINT [DF_ShortenUrls_InsertDate]  DEFAULT (getdate()) FOR [InsertDate]
END

GO


